var app = getApp();
var http_service = app.data.service;
Page({
  data: {
    http_service: http_service,
    date: '暂未选择',
    storeId: 0,
    store_name: '',
    serviceId:0,
    price:0,
    service_name:'',
    tec_id:0,
    technician:{},
    inputval:'',//输入框的值
    click:true,//是否可以点击
    show_price:0
  },
  onLoad: function (options) {
    var that =this;
    //最终选择的serviceId
    var serviceId = options.serviceId;
    //最终选择的门店id
    var storeId = options.storeId;
    // console.log(serviceId)
    // console.log(storeId)
    that.setData({
      serviceId:serviceId,
      storeId:storeId
    })


    wx.request({
      url: http_service +  '/index.php/getstorebyid',
      data:{
        storeId:storeId
      },
      success:function(res){
        that.setData({
          store_name:res.data.store_name
        })
      }
    })


    wx.request({
      url: http_service + '/index.php/getservicebyid',
      data:{
        serviceId:serviceId
      },
      success: function (res) {
        that.setData({
          storeId: storeId,
          serviceId: serviceId,
          price: res.data.price,
          tec_id: 0,
          service_name: res.data.service_name,
          date: '未选择'
        });
      }
    })

    //请求电话号码，用于预填写在输入框
    wx.getStorage({
      key: 'userid',
      success: function(res) {
        wx.request({
          url: http_service + '/index.php/getphone?id=' + res.data,
          success:function(res){

            if(res.data){
              that.setData({
                inputval: res.data.phone
              })
            }else{
              inputval: ""
            }
            
          }
        })
      },
    })
  },
/**
 * 显示
 */
  onShow:function(){
    if(app.data.hide){
      wx.request({
        url: http_service + '/index.php/apideleteorder',
        data: {
          orderId: app.data.orderId
        },
        success:function(res){
          wx.reLaunch({
            url: '/pages/index/subscribe/childservice1'
          })
        }
      })
      return;
    }
    var that = this;
    wx.getStorage({
      key: 'affirm',
      success: function(res) {
        that.setData({
          storeId: res.data.storeId,
          serviceId: res.data.serviceId,
          price: res.data.price,
          show_price: res.data.price,
          tec_id: res.data.tec_id,
          date: res.data.date,
          c_date: res.data.c_date,
          c_time: res.data.c_time
        },function(){
            //读取本地数据成功之后开始请求技师，
            //如果没有选择时间就不推荐，选择有时间就推荐一个最有空的
          if (that.data.date!="未选择"){
            if (res.data.tec_id == 0) {
              wx.request({
                url: http_service + '/index.php/gettechnician1',
                data: {
                  store_id: res.data.storeId,
                  serviceId:that.data.serviceId,
                  c_date : that.data.c_date,
                  c_time :that.data.c_time
                },
                success: function (res) {
                  //console.log(res)
                  if (res.data) {
                    that.setData({
                      tec_id: res.data.id,
                      technician: res.data
                    })
                  } else {
                    that.setData({
                      tec_id: 0,
                      technician: {
                        img:''
                      }
                    })
                  }
                }
              })
            } else{
              wx.request({
                url: http_service + '/index.php/gettechnician1',
                data: {
                  tec_id: res.data.tec_id
                },
                success: function (res) {
                    that.setData({
                      tec_id: res.data.id,
                      technician: res.data
                    })
                }
              })
            }
          }
        })
      },
    })
  },
 
  //获取输入框值
  bindKeyInput: function (e) {
    this.setData({
      inputval: e.detail.value
    })
  },


  //点击预约
  doAffirm:function(e){
    var that = this;
    
    var a= that.data.inputval;
    if(!that.data.click){
      return;
    }
    that.setData({
      click: false
    })
    console.log(that.data.tec_id)
    if (that.data.tec_id==0||that.data.c_time == 0 || a == null || a == "" || a.length != 11){
      //console.log("机那里")
      wx.showModal({
        title: '信息不全',
        content: '未选择时间或手机号码不正确！',
      })
      that.setData({
        click: true
      })
    }else{
      var userid = 0;
      wx.getStorage({
        key: 'userid',
        success: function(res) {
          //读取到userid
          var userId=res.data;

          var storeId=that.data.storeId;
          var serviceId=that.data.serviceId;
          var tecId=that.data.tec_id;
          var c_date=that.data.c_date;
          var c_time=that.data.c_time;
          var phone=that.data.inputval;

          wx.request({
            url: http_service + '/index.php/wxpay',
            data: {
              userId: userId,
              storeId: storeId,
              serviceId: serviceId,
              tecId: tecId,
              c_date: c_date,
              c_time: c_time,
              phone: phone,
            },
            fail:function(){
              //console.log("统一下单失败")
            },
            success: function (res) {
              //console.log(res.data)
              if (res.data.state==0){
                wx.showModal({
                  title: '预约失败',
                  content: res.data.text,
                })
              }
             
              if (res.data.state==1) {
                var out_trade_no = res.data.out_trade_no;
                var prepay_id = res.data['prepay_id'];
                //console.log(prepay_id);
                wx.requestPayment({
                  timeStamp: res.data['timeStamp'],
                  nonceStr: res.data['nonceStr'],
                  package: res.data['package'],
                  signType: 'MD5',
                  paySign: res.data['paySign'],
                  'fail':function(){
                      that.setData({
                        click:true
                      })
                  },
                  'success': function () {
                    //console.log("支付成功")//支付成功之后开始
                    wx.getStorage({
                      key: 'userid',
                      success: function(res) {
                        wx.request({
                          url: http_service + '/index.php/doaffirm',
                          data: {
                            store_id: that.data.storeId,
                            service_id: that.data.serviceId,
                            price: that.data.price,
                            tec_id: that.data.tec_id,
                            c_date: that.data.c_date,
                            c_time: that.data.c_time,
                            phone: that.data.inputval,
                            user_id: res.data,
                            out_trade_no: out_trade_no,
                            prepay_id: prepay_id
                          },
                          success: function (res) {
                            //console.log(res.data)
                            var orderId = res.data['orderId']

                            
                            if (res.data.rescode > 0) {
                              
                             
                              

                              wx.showModal({
                                title: '成功',
                                content: "预约成功！！",
                                success: function (res) {

                                  wx.request({
                                    url: http_service + '/index.php/sendtemplate',
                                    data: {
                                      prepay_id: prepay_id,
                                      orderId: orderId
                                    }
                                  })
                                  wx.reLaunch({
                                    url: '/pages/index/subscribe/service'
                                  })
                                }
                              })
                            } else {
                              wx.showModal({
                                title: '预约失败',
                                content: '尝试重新预约！',
                              })
                            }
                          }
                        })
                      },
                    })
                  },
                })
              }
            }
          })
        },
      })
      
    }
  },

  /**
   * 更换时间
   */
  changeDate:function(){
    var that = this;
    var storeId = that.data.storeId;
    var store_name = that.data.store_name;
    var serviceId =  that.data.serviceId;
    var service_name = that.data.service_name;
    var price = that.data.price;
    var tec_id = that.data.tec_id;
    wx.navigateTo({
      url: 'date?storeId=' + storeId + "&store_name=" + store_name + "&serviceId=" + serviceId + "&service_name=" + service_name + "&price=" + price +"&tec_id=" +tec_id,
    })
  },
  /**
   * 更换技师
   */
  changeTechnician:function(){
    var that = this;
    var storeId = that.data.storeId;
    var store_name = that.data.store_name;
    var serviceId = that.data.serviceId;
    var service_name = that.data.service_name;
    var price = that.data.price;
    var tec_id = that.data.tec_id;
    var c_date = that.data.c_date;
    var c_time = that.data.c_time;
    var date = that.data.date;
    //console.log(date)
    if (that.data.date == "未选择"){
        wx.showModal({
          title: '未选择时间',
          content: '请先选择时间后再试',
        })
    }else{
      wx.navigateTo({
        url: 'technician?storeId=' + storeId + "&store_name=" + store_name + "&serviceId=" + serviceId + "&service_name=" + service_name + "&price=" + price + "&tec_id=" + tec_id + "&c_time=" + c_time + "&c_date=" + c_date + "&date=" + date,
      })
    }
  },



  /**
   * 点击预约
   *    这里仅仅下单
   */
  order:function(){
    var that = this;

    var a = that.data.inputval;
    if (!that.data.click) {
      return;
    }
    that.setData({
      click: false
    })
    //判断是否选择时间和是否填写电话号码
    if (that.data.tec_id==0||that.data.c_time == 0 || a == null || a == "" || a.length != 11) {
      var title="";
      var content="";
      if (that.data.c_time == 0){
          title="时间错误";
          content="未选择时间"
      } else if (that.data.tec_id == 0){
        title = "技师错误";
        content = "未选择技师"
      } else if (a == null || a == "" || a.length != 11 ){
        title = "电话号码错误";
        content = "请正确填写手机号"
      }
      wx.showModal({
        title: title,
        content: content,
      })
      that.setData({
        click: true
      })
    }else{
      //信息填写正确，下单到系统
      wx.getStorage({
        key: 'userid',
        success: function (res) {
          wx.request({
            url: http_service + '/index.php/placeanorder',
            data: {
              store_id: that.data.storeId,
              service_id: that.data.serviceId,
              price: that.data.price,
              tec_id: that.data.tec_id,
              c_date: that.data.c_date,
              c_time: that.data.c_time,
              phone: that.data.inputval,
              user_id: res.data,
            },
            success:function(res){
              //rescode == 0--成功下单 -1---时间冲突下单失败
              var msg = res.data.msg;
              if(res.data.rescode == 0){
                wx.navigateTo({
                  url: 'placeOrder?orderId='+res.data.orderId,
                })
              }else if(res.data.rescode == -1){
                that.setData({
                  click: true
                },function(){
                  wx.showModal({
                    title: '下单失败',
                    content: msg,
                  })
                })
              }
            },
            error:function(){
              wx.showToast({
                title: '错误',
                success:function(){
                  wx.reLaunch({
                    url: '/pages/index/subscribe/service'
                  })
                },
                fail:function(){
                  wx.reLaunch({
                    url: '/pages/index/subscribe/service'
                  })
                }
              })
            }
          })
        }
      })  
    }

  },

  // 当用户该表选择有误产品的时候
  radioChange:function(e){
    //console.log('radio发生change事件，携带value值为：', e.detail.value);
    var that = this;
    if (e.detail.value==1){
        that.setData({
          price:0.1
        })
    }else{
        that.setData({
          price: that.data.show_price
        })  
      }

    }



})  
